﻿/* ***********************************************
 * Author		:  kingthy
 * Email		:  kingthy@gmail.com
 * Description	:  Types
 *
 * ***********************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace KT.Core.Extensions
{
    /// <summary>
    /// 与类型相关的扩展方法
    /// </summary>
    public static class Types
    {
        /// <summary>
        /// 获取类型对应的System.Data.DbType值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DbType GetDbType(this Type type)
        {
            var tc = Type.GetTypeCode(type);
            switch (tc)
            {
                case TypeCode.Char:
                    return DbType.StringFixedLength;
                case TypeCode.DBNull:
                case TypeCode.Empty:
                    return DbType.Object;
                case TypeCode.Object:
                    //处理特殊的类型
                    if (type.Equals(typeof(Guid))) return DbType.Guid;
                    if (type.IsArray && type.Equals(typeof(byte[]))) return DbType.Binary;
                    return DbType.Object;
                default:
                    return tc.ToString().As<DbType>();
            }
        }
    }
}
